/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.util.StringUtils;
import cn.qidisoft.core.web.customargument.bind.annotation.IfNeedLogin;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.model.Priority;
import cn.qidisoft.edu.hzjt.model.Student;
import cn.qidisoft.edu.hzjt.model.SysLog;
import cn.qidisoft.edu.hzjt.model.User;
import cn.qidisoft.edu.hzjt.model.UserEmployee;
import cn.qidisoft.edu.hzjt.service.ICommonService;
import cn.qidisoft.edu.hzjt.service.ILoggerService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 用户登录
 * 
 * @author hxq
 * @version $Id: LoginController.java, v 0.1 2018年10月19日 下午4:17:49 hxq Exp $
 */
@Controller
public class LoginController {
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);
  @Autowired
  private ICommonService commonService;
  @Autowired
  private ILoggerService loggerService;


  @RequestMapping(value = "index")
  public String index(HttpServletRequest request) throws Exception {
    return "login";
  }

  /**
   * 登录
   * 
   * @param request
   * @param response
   * @return
   */

  @RequestMapping(value = "login")
  @IfNeedLogin(needLogin = false)
  public String login(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    try {
      String username = request.getParameter("username");
      // String password = request.getParameter("password");
      String type = request.getParameter("type");
      if (!StringUtils.isNullValue(type) && "s".equals(type)) { // 代表学生
        Student student = new Student();
        student.setXsid(Integer.parseInt(username));
        Student currentLoginedStudent = commonService.queryStudentByEmployee(student);
        request.getSession().setMaxInactiveInterval(1800);
        request.getSession().setAttribute(Constant.LOGIN_CONSOLE_USER, currentLoginedStudent);
        request.getSession().setAttribute(Constant.LOGIN_USER_TYPE, Constant.USER_TYPE_STUDENT);
        SysLog loggerReq = new SysLog();
        loggerReq.setOperator(student.getXsid());
        loggerReq.setIphost(request.getRemoteAddr());
        loggerReq.setTarget("用户登录");
        loggerReq.setBehaviour("登录");
        loggerReq.setDetailinfo("用户登录信息");
        loggerReq.setLogtime(new Date());
        loggerService.addLogger(loggerReq);
        return "index";
      } else if (!StringUtils.isNullValue(type) && "t".equals(type)) {
        UserEmployee user = new UserEmployee();
        user.setUid(Integer.parseInt(username));
        UserEmployee currentLoginedUser = commonService.queryUserByEmployee(user);
        request.getSession().setMaxInactiveInterval(1800);
        request.getSession().setAttribute(Constant.LOGIN_CONSOLE_USER, currentLoginedUser);
        request.getSession().setAttribute(Constant.LOGIN_USER_TYPE, Constant.USER_TYPE_TEACHER);
        // 权限信息
        List<Priority> priorityList = commonService.queryUserByPriority(user);
        request.getSession().setAttribute("permisstionList", priorityList);
        SysLog loggerReq = new SysLog();
        loggerReq.setOperator(user.getUid());
        loggerReq.setIphost(request.getRemoteAddr());
        loggerReq.setTarget("用户登录");
        loggerReq.setBehaviour("登录");
        loggerReq.setDetailinfo("用户登录信息");
        loggerReq.setLogtime(new Date());
        loggerService.addLogger(loggerReq);
        return "index";
      } else {
        return "login";
      }
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("登录信息 login [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), e.getMessage());
      model.addAttribute(Constant.EXPTION_MSG_KEY, e);
      return "error/error";
    }

  }


  /**
   * 注销
   * 
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "logout")
  public String logout(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    try {
      User user = (User) request.getSession().getAttribute(Constant.LOGIN_CONSOLE_USER);
      String username = null;
      if (request.getSession().getAttribute(Constant.LOGIN_CONSOLE_USER) != null) {
        user = (User) request.getSession().getAttribute(Constant.LOGIN_CONSOLE_USER);
        username = user.getName();
        request.getSession().removeAttribute(Constant.LOGIN_CONSOLE_USER);
      }
      request.setAttribute("username", username);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("调用logout()服务器异常", e);
      model.addAttribute(Constant.EXPTION_MSG_KEY, e);
      return "error/error";
    }
    return "login";

  }
}
